﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;
using Microsoft.Reporting.WebForms;
namespace App.Controllers
{
    public class CheckoutServiceController : BaseController
    {
        
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            //int offset = (page ?? 1 - 1) * (rows ?? 10);
            List<vi_ServiceDetail> list = null;
            //默认选择最后一个死者
            var dead = dbe.tb_RegistrationInfo.OrderByDescending(m => m.DeadNO).FirstOrDefault();
            if (dead == null) {
                return Json(new List<vi_ServiceDetail>(), JsonRequestBehavior.AllowGet);
            }
            if ("asc" == order)
            {
                list = dbe.vi_ServiceDetail.Where(m=> m.DeadNO==dead.DeadNO).OrderBy(m => m.DeadNO).ToList();
            }
            else
            {
                list = dbe.vi_ServiceDetail.OrderByDescending(m => m.DeadNO).ToList();
            }
            return Json(list, JsonRequestBehavior.AllowGet);
            
        }

        //根据死者编号查询
        public ActionResult Search(FormCollection collection)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            string deadNo = collection["DeadNO"];
            //string deadName = collection["DeadName"];

            var list = dbe.vi_ServiceDetail.Where(m => m.DeadNO == deadNo
                            //|| (deadName.Trim() != "" && m.DeadName.Contains(deadName))
                            )
                            .OrderBy(m => m.DeadNO);
            return Json(new {success=true,msg=list}, JsonRequestBehavior.AllowGet);
        } 

        public ActionResult DiscountList(int? id) {
            // 禁止页面被缓存
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Today.AddYears(-2));
            string DeadNO = Request.Form["DeadNO"];
            string DeadName = Request.Form["DeadName"];
            
            PagedList<App.Models.vi_ServiceDetail> vhList = dbe.vi_ServiceDetail
                .Where(m => m.DeadNO.Equals(DeadNO) || ((DeadName != "") && m.DeadName.Contains(DeadName)))
                .OrderByDescending(m => m.DeadNO)
                .ToPagedList(id ?? 1, int.MaxValue);
            if (Request.IsAjaxRequest())
                return PartialView("UCDiscountServiceList", vhList);
            return View(vhList);
        }
        //进行数量折扣
        public ActionResult Discount(string serviceNo, int qty, decimal fee)
        {
            try
            {
                var sd = dbe.tb_ServiceDetail.FirstOrDefault(m => m.ServiceNo == serviceNo);
                sd.ServiceCount = qty;
                sd.ServiceFee = fee;
                dbe.SaveChanges();
                return Content(string.Format("{0:f}", fee));
            }
            catch {
                return Content("err");
            }
        }
        //收费
        [HttpPost]
        public ActionResult Pay(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                DateTime dt = DateTime.Now;
                List<vi_ServiceDetail> list = new List<vi_ServiceDetail>();
                foreach (var id in idList)
                {
                    var o = dbe.tb_ServiceDetail.FirstOrDefault(m => m.ServiceNo == id);
                    if (o.PayStatus) continue;
                    o.PayStatus = true;
                    if (o.ServiceCountBak == null) o.ServiceCountBak = o.ServiceCount;
                    o.PayTime = dt;
                    o.Cashier = CurrentUser;                      
                }
                dbe.SaveChanges();
                foreach (var id in idList)
                {
                    list.Add(dbe.vi_ServiceDetail.FirstOrDefault(m => m.ServiceNo == id));
                }
                return Json(new { success = true, msg = list }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false, msg = "收费出错！" }, JsonRequestBehavior.AllowGet);
            }   
        }
        //打印死者费用列表
        public ActionResult PrintDeadFeeList()
        {               
            return View();
        }

        //打印年、月、日收入报表
        public ActionResult PrintFeeReport() {
            return View();
        }
       
    }
}
